﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Entities.Codes;

namespace Pg.BioMedics.SDR.Services
{
    /// <summary>
    /// Provides static routines for content processing
    /// </summary>
    internal static class ContentHelper
    {
        #region Constants

        private const string JPEGExtension = ".jpg";
        private const string WAVExtension = ".wav";
        private const string TXTExtension = ".txt";

        #endregion

        #region Static methods

        /// <summary>
        /// Prepares content stored in temporary file. This content has been 
        /// temopraly stored from the file defined in the first argument.
        /// </summary>
        /// <param name="fileName">File name</param>
        /// <param name="temporaryLocation">Temporary location</param>
        /// <returns>Content instance</returns>
        internal static Content PrepareContent(string fileName, string temporaryLocation)
        {
            Content content = new Content();
            content.Data = File.ReadAllBytes(temporaryLocation);
            content.TypeCode = ResolveContentType(fileName);
            content.FileName = Path.GetFileName(fileName);

            return content;
        }

        /// <summary>
        /// Returns content type code resolved basing on file name extension
        /// </summary>
        /// <param name="fileName">File name</param>
        /// <returns>Content type code</returns>
        internal static string ResolveContentType(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
                return ContentTypeCodes.RAW;

            string extension = Path.GetExtension(fileName);
            if (extension.ToLower() == WAVExtension)
                return ContentTypeCodes.WAV;
            if (extension.ToLower() == JPEGExtension)
                return ContentTypeCodes.JPEG;
            else if (extension.ToLower() == TXTExtension)
                return ContentTypeCodes.TEXT;
            else
                return ContentTypeCodes.RAW;
        }

        #endregion
    }
}
